package com.wangsd.common.mq;


import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.wangsd.common.component.WeixinPayService;
import com.wangsd.common.component.WxPayServiceExt;
import com.wangsd.common.constant.MqConstant;
import com.wangsd.web.face.entity.Merchant;
import com.wangsd.web.face.entity.TOrder;
import com.wangsd.web.face.service.MemberService;
import com.wangsd.web.face.service.MerchantService;
import com.wangsd.web.face.service.TOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * facePay订单查询
 */

@Component
@RabbitListener(queues = MqConstant.RABBITMQ_FACEPAY_ORDER_QUERY)
@Slf4j
public class OrderQueryReceive {

    @Autowired
    WeixinPayService weixinPayService;
    @Autowired
    MerchantService merchantService;
    @Autowired
    MqSend mqSend;
    @Autowired
    TOrderService orderService;
    @Autowired
    MemberService memberService;


    @RabbitHandler
    public void process(Long orderId) {
        //获取实时最新订单
        TOrder order = orderService.getById(orderId);
        log.info(order.getOutTradeNo() + "执行订单查询");


        Merchant merchant = merchantService.getById(order.getMerchantId());
        if (merchant == null) {
            log.info(order.getOutTradeNo() + "查询订单，商户信息异常");
            return;
        }
        try {
            //查询订单
            WxPayServiceExt wxPayService = weixinPayService.getServiceExt(order.getCode(), merchant.getSubMchId());
            WxPayOrderQueryRequest queryRequest = new WxPayOrderQueryRequest();
            queryRequest.setTransactionId(order.getTransactionId());
            WxPayOrderQueryResult wxPayOrderQueryResult = wxPayService.queryOrder(queryRequest);

            //处理订单查询结果
            if (WxPayConstants.ResultCode.SUCCESS.equals(wxPayOrderQueryResult.getReturnCode())//请求成功
                    && WxPayConstants.ResultCode.SUCCESS.equals(wxPayOrderQueryResult.getResultCode())) {//查询结果成功
                TOrder newOrder = orderService.getById(order.getId());
                newOrder.setTimeEnd(wxPayOrderQueryResult.getTimeEnd());
                //交易成功
                if (WxPayConstants.WxpayTradeStatus.SUCCESS.equals(wxPayOrderQueryResult.getTradeState())) {
                    //支付成功
                    newOrder.setOrderStatus(1);
                    newOrder.setRealPayFee(Long.valueOf(wxPayOrderQueryResult.getCashFee()));
                    //修改订单状态
                    orderService.saveOrUpdate(newOrder);

                } else {
                    //交易失败
                    if (WxPayConstants.WxpayTradeStatus.PAY_ERROR.equals(wxPayOrderQueryResult.getTradeState())) {
                        //支付失败
                        newOrder.setOrderStatus(2);
                        //修改订单状态
                        orderService.saveOrUpdate(newOrder);
                    }
                }
                return;
            }
            log.error(order.getOutTradeNo() + "订单查询失败！！");
            return;
        } catch (WxPayException e) {
            e.printStackTrace();
        }
    }
}
